package com.example.lhsama.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.lhsama.dao.OrdersMapper;
import com.example.lhsama.domain.Orders;
import com.example.lhsama.domain.Product;
import com.example.lhsama.service.OrdersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @Date: 2021年09月19日 15:19
 * @Author: LHSAMA
 */
@Transactional
@Service
public class OrdersServiceImpl extends ServiceImpl<OrdersMapper, Orders> implements OrdersService {
    @Autowired
    private OrdersMapper ordersMapper;

    @Override
    public List<Orders> findAll() {
        return ordersMapper.findAll();
    }

    @Override
    public List<Product> findProductsByOrdersId(Integer id) {
        List<Product> products = ordersMapper.findProductsByOrdersId(id);
        for (Product product : products) {
            product.setCount(ordersMapper.countProducts(product.getId(),id));
        }
        return products;
    }

    @Override
    public Orders findById(Integer id) {
        List<Product> products = findProductsByOrdersId(id);
        Orders byId = ordersMapper.findById(id);
        byId.setProducts(products);
        return byId;
    }


}
